package com.project.XXXX.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.project.XXXX.controllerInterface.ICityChoosenController;
import com.project.XXXX.inter.tbl_divisionMapper;
import com.project.XXXX.model.tbl_division;
import com.project.XXXX.tools.ProjectTools;

@Controller

public class CityChoosenControllerImpl implements ICityChoosenController {

	@Resource
	private tbl_divisionMapper tbl_divisionMapper;

	@RequestMapping("/getProvince.do")
	public void getprovince(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		List<tbl_division> list = null;
		list = tbl_divisionMapper.selectProvinces();
		Map<String, String> map = new HashMap<>();
		String json;
		if (list != null) {
			json = new Gson().toJson(list);
		} else {
			map.put("state", "error");
			map.put("error_description", "parameter error");
			json = new Gson().toJson(map);
		}

		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/getCities.do")
	public void getCities(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {
		List<tbl_division> list = null;
		String Province = request.getParameter("Province");
		 if(StringUtils.length(Province)==6)
	       {
			  Province=Province.substring(0,2)+"%";
		list = tbl_divisionMapper.selectCitysByProvince(Province);
	       }
		Map<String, String> map = new HashMap<>();
		String json;
		if (list != null) {
			json = new Gson().toJson(list);
		} else {
			map.put("state", "error");
			map.put("error_description", "parameter error");
			json = new Gson().toJson(map);
		}
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/getCountries.do")
	public void getCountries(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		List<tbl_division> list = null;
		Map<String, String> map = new HashMap<>();
		String json;
		String City = request.getParameter("City");

       if(StringUtils.length(City)==6)
       {
    	   if( StringUtils.startsWithAny(City, "31","50","11","12"))
    	   {
    		City=City.substring(0, 2)+"%";
    	   }
    	   else
    	   {
    	   City=City.substring(0, 4)+"%";
    	   }
		list = tbl_divisionMapper.selectCountriesByCity(City);
       }
		if (list != null) {
			json = new Gson().toJson(list);
		} else {
			map.put("state", "error");
			map.put("error_description", "parameter error");
			json = new Gson().toJson(map);
		}

		ProjectTools.ResponseOut(response, json);

	}

	
	@RequestMapping("/getPlaceByName.do")
	public void getPlaceCodeByName(@RequestParam("PlaceName") String PlaceName,HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		Map<String, String> map = new HashMap<>();
	   tbl_division tb = tbl_divisionMapper.getByPlaceName(PlaceName);
		String json;
		if (tb != null) {
			json = new Gson().toJson(tb);
		} else {
			map.put("state", "error");
			map.put("error_description", "parameter error");
			json = new Gson().toJson(map);
		}

		ProjectTools.ResponseOut(response, json);
	}
}
